{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Vue + rabbitmq 搭建\n",
    "\n",
    "我是直接用rabbitmq的docker以命令： \n",
    "```shell\n",
    "docker run -di --name=rabbitmq-stomp -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=toto -p 5617:5617 -p 5672:5672 -p 4369:4369 -p 15671:15671 -p 15672:15672 -p 25672:25672 -p 15670:15670 -p 15674:15674 rabbitmq:3.7.16-management \n",
    "```\n",
    "运行的， 启动docker后需要进入docker 运行：\n",
    "```\n",
    "rabbitmq-plugins enable rabbitmq_web_stomp\n",
    "rabbitmq-plugins enable rabbitmq_web_stomp_examples\n",
    "```\n",
    "\n",
    "### 这里遇到了好几个坑：\n",
    "\n",
    "1. 启动rabbitmq后， 一直链接不上， 原因是没有打开15674端口\n",
    "2. 打开了15674端口后，能连接了，但是一直显示 refuse user xxx的字样， 很烦。 最后想想大概还是端口的问题，索性把所有端口都映射一次。 结果还是不行，我用原生js试了一次，其实是可以链接的，就是在 vue 的框架里不行。\n",
    "3. 原因在于： \n",
    "```javascript\n",
    "this.client.connect(mqlogin, mqpassword, this.onConnected, this.onFailed, '/')\n",
    "```\n",
    "这一段代码中的Login和pass不能用headers这种key:value的形式替代，可能会无法识别。 \n",
    "\n",
    "\n",
    "贴一下vue的代码：\n",
    "```javascript\n",
    "onConnected(frame) {\n",
    "        console.log(\"subscribe!\")\n",
    "        // this.client.subscribe(pushEx);\n",
    "        this.client.subscribe(pushEx, this.responseCallback, this.onFailed)\n",
    "        console.log(frame)\n",
    "        this.isMQConnected = true\n",
    "      },\n",
    "\n",
    "      onFailed(frame) {\n",
    "         console.log(\"Failed: \" + frame)\n",
    "         this.isMQConnected = false\n",
    "      },\n",
    "\n",
    "      responseCallback(frame) {\n",
    "            console.log(\"得到的消息 msg=>\" + frame.body);\n",
    "            console.log(frame)\n",
    "            //接收到服务器推送消息，向服务器定义的接收消息routekey路由rk_recivemsg发送确认消息\n",
    "            this.client.send(recieveEx, {\"content-type\":\"text/plain\"}, frame.body);\n",
    "      },\n",
    "\n",
    "      connect() {\n",
    "        this.client = Stomp.client(mqService)\n",
    "        this.client.connect(mqlogin, mqpassword, this.onConnected, this.onFailed, '/')\n",
    "      }\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
